package net.lastcoder.ifserver.node;

import net.lastcoder.ifserver.module.BaseModule;
import net.lastcoder.ifserver.module.ModuleNames;
import net.lastcoder.ifserver.session.SessionManager;

import java.util.HashMap;

/**
 * auth: WenYF
 * date: 2017/2/10
 */
public class NodeManager extends BaseModule {

    private HashMap<Character, NodesHolder> nodes;

    public NodeManager() {
        super(ModuleNames.NodeManager);
    }

    @Override
    public void init() {
        nodes = new HashMap<>();
    }

    @Override
    public void reload() {
        nodes.clear();
    }

    @Override
    public void destroy() {

    }

    private static class SingletonHolder {
        static NodeManager instance = new NodeManager();
    }

    public static NodeManager getInstance() {
        return SingletonHolder.instance;
    }

    public void removeNodesHolder(NodesHolder holder) {
        nodes.remove(holder.getResource());
    }

    public void addNodesHolder(char resource, NodesHolder nodesHolder) {
        nodes.put(resource, nodesHolder);
    }
    public Node getNode(char resource, String username) {
        NodesHolder nodesHolder = nodes.get(resource);
        if (nodesHolder != null) {
            return nodesHolder.getNode(username);
        }
        return null;
    }
}
